#!/usr/bin/env node

/*
 * pemtojwk - convert PEM PKCS#5/#8 private/public key to JWK file
 *
 * Copyright (c) 2016 Kenji Urushima (kenji.urushima@gmail.com)
 *
 * This software is licensed under the terms of the MIT License.
 * https://kjur.github.io/jsrsasign/license
 *
 * The above copyright and license notice shall be 
 * included in all copies or substantial portions of the Software.
 * 
 * Please use '-h' option for this script usage.
 * ---------------------------------------------------------
 * DESCRIPTION
 *   This script converts from PEM PKCS#5/#8 RSA/ECC private/public
 *   key file to RFC 7517 JSON Web Key (JWK) file.
 *
 * USAGE
 *   % pemtojwk [INPUT PEM KEY FILE] [OUTPUT JWK FILE]
 *   % pemtojwk aaa.prv.pem aaa.prv.jwk
 */

var program = require('commander');
var rs = require('jsrsasign');
var rsu = require('jsrsasign-util');
var path = require('path');

program
  .version('1.0.1 (2016-Sep-11)')
  .description('convert PEM PKCS#5/#8 private/public key to JWK')
  .usage('[INPUT PEM KEY] [OUTPUT JWK]')
  .parse(process.argv);

if (program.args.length !== 2)
  throw "wrong number of args"

var inFile = program.args[0];
var outFile = program.args[1];

var pem = rsu.readFile(inFile);
var key = rs.KEYUTIL.getKey(pem);
var jwk = rs.KEYUTIL.getJWKFromKey(key);
rsu.saveFile(outFile, JSON.stringify(jwk, null, 2));



